package com.shengsiyuan.nio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NioTest04 {

    public static void main(String arg[]) throws Exception {

        FileInputStream fileInputStream = new FileInputStream("C:\\Users\\Administrator\\IdeaProjects\\Netty-Learning\\src\\main\\java\\com\\shengsiyuan\\nio\\text\\input1.txt");
        FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\Administrator\\IdeaProjects\\Netty-Learning\\src\\main\\java\\com\\shengsiyuan\\nio\\text\\output.txt");

        FileChannel inputChannel = fileInputStream.getChannel();
        FileChannel outputChannel = fileOutputStream.getChannel();

        ByteBuffer buffer = ByteBuffer.allocate(521);

        while (true) {
            //buffer.clear();
            int read = inputChannel.read(buffer);// 返回读取的字节的个数，没有返回-1
            System.out.println(read);
            if (-1 == read) {
                break;
            }
            buffer.flip();
            outputChannel.write(buffer);
        }

        fileInputStream.close();
        fileOutputStream.close();
    }

}
